package com.yyfax.common.toast;

import android.content.Context;
import android.support.v4.app.NotificationManagerCompat;
import android.widget.Toast;

/**
 * 自定义的Toast类, 系统关闭通知消息仍能显示.
 */
public class CompatToast {
    static final int LENGTH_SHORT = 0;
    static final int LENGTH_LONG = 1;
    private static final int SYSTEM_TOAST_ENABLED = 0;
    private static final int SYSTEM_TOAST_DISABLED = 1;
    private Object mToast;

    private CompatToast(Context context, String message, int duration) {
        try {
            if (isNotifyDisabled(context)) {
                mToast = CustomToast.makeText(context, message, duration);
            } else {
                mToast = Toast.makeText(context, message, duration);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private CompatToast(Context context, int textId, int duration) {
        try {
            if (isNotifyDisabled(context)) {
                mToast = CustomToast.makeText(context, textId, duration);
            } else {
                mToast = Toast.makeText(context, textId, duration);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isNotifyDisabled(Context context) {
        boolean isDisabled = false;
        int checkNotification = isNotificationEnabled(context)
                ? SYSTEM_TOAST_ENABLED : SYSTEM_TOAST_DISABLED;
        if (checkNotification == SYSTEM_TOAST_DISABLED) {
            isDisabled = true;
        }

        return isDisabled;
    }

    private static CompatToast makeText(Context context, String message, int duration) {
        return new CompatToast(context, message, duration);
    }

    private static CompatToast makeText(Context context, int textId, int duration) {
        return new CompatToast(context, textId, duration);
    }

    public static void showShortToast(Context context, int textId) {
        makeText(context, textId, LENGTH_SHORT).show();
    }

    public static void showShortToast(Context context, String msg) {
        makeText(context, msg, LENGTH_SHORT).show();
    }


    public static void showLongToast(Context context, int textId) {
        makeText(context, textId, LENGTH_LONG).show();
    }

    public static void showLongToast(Context context, String msg) {
        makeText(context, msg, LENGTH_LONG).show();
    }

    public static void showDurationToast(Context context, String msg, int duration) {
        makeText(context, msg, duration);
    }

    public static void showDurationToast(Context context, int textId, int duration) {
        makeText(context, textId, duration);
    }

    /**
     * 判断是否开启通知权限
     */
    private static boolean isNotificationEnabled(Context context) {
        // 默认小于19使用自定义Toast
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
            return false;
        }
        // 该方法仅在19(4.4)及以上有效, 以下会默认返回true, 可通过上面注释代码默认返回false.
        return NotificationManagerCompat.from(context).areNotificationsEnabled();
    }

    private void show() {
        try {
            if (mToast instanceof CustomToast) {
                ((CustomToast) mToast).show();
            } else if (mToast instanceof Toast) {
                ((Toast) mToast).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
